//
//  JUserInfoController.m
//  HeadIndustry
//
//  Created by jun on 2017/9/12.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JUserInfoController.h"

@interface JUserInfoController ()

@property (nonatomic, strong) NSMutableArray *textFieldArr;

@end

@implementation JUserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super InitUI];
    self.titleLabel.text = @"个人资料";
    self.leftButton.hidden = NO;
    self.rightButton. hidden = YES;
    [self.leftButton setImage:[UIImage imageNamed:backBtnImg] forState:UIControlStateNormal];
    self.leftButton.frame = CGRectMake(8, self.leftButton.frame.origin.y, 30, 30);
    
    [self setConfigbgview];
}

- (void)setConfigbgview {
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, kWindowW, 100)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:topView];
    
    YYLabel *label1 = [YYLabel new];
    label1.top = topView.top;
    label1.left = topView.left + 15;
    label1.size = CGSizeMake(80, 50);
    label1.text = @"昵称";
    label1.font = kFONT_TITLE(16);
    [topView addSubview:label1];
    
    NSString *nickname = [GLOBALDATA userInfoModel].NickName;
    YYLabel *righelabel1 = [YYLabel new];
    righelabel1.top = topView.top;
    righelabel1.left = label1.right + 10;
    righelabel1.size = CGSizeMake(topView.width - 25 - label1.right, 50);
    righelabel1.text = nickname;
    righelabel1.textAlignment = NSTextAlignmentRight;
    righelabel1.font = kFONT_TITLE(16);
    [topView addSubview:righelabel1];
    
    CALayer *line1 = [CALayer layer];
    line1.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line1.frame = CGRectMake(15, label1.bottom - 0.5, kWindowW - 30, 0.5);
    [topView.layer addSublayer:line1];
    
    YYLabel *label2 = [YYLabel new];
    label2.top = label1.bottom;
    label2.left = label1.left ;
    label2.size = CGSizeMake(80, 50);
    label2.text = @"性别";
    label2.font = kFONT_TITLE(16);
    [topView addSubview:label2];
    
    NSInteger sex = [GLOBALDATA userInfoModel].Sex;
    NSString *sexStr;
    if (sex == 1) {
        sexStr = @"男";
    }
    else
    {
        sexStr = @"女";
    }
    
    YYLabel *righelabel2 = [YYLabel new];
    righelabel2.top = righelabel1.bottom;
    righelabel2.left = righelabel1.left;
    righelabel2.size = CGSizeMake(righelabel1.width, 50);
    righelabel2.text = sexStr;
    righelabel2.textAlignment = NSTextAlignmentRight;
    righelabel2.font = kFONT_TITLE(16);
    [topView addSubview:righelabel2];
    
    UIView *midView = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom + 10, kWindowW, 3*50)];
    midView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:midView];
    
    NSArray *titleArr = @[@"姓名",@"电话",@"Email"];
    NSArray *placeholderArr = @[@"未填写",@"未填写",@"未填写"];
    
    NSString *nameStr;
    NSString *telStr;
    NSString *emailStr;
    if ([GLOBALDATA userInfoModel].Username.length > 0) {
        nameStr = [GLOBALDATA userInfoModel].Username;
    }
    else
    {
        nameStr = @"0";
    }
    if ([GLOBALDATA userInfoModel].Tel.length > 0) {
        telStr = [GLOBALDATA userInfoModel].Tel;
    }
    else
    {
        telStr = @"0";
    }if ([GLOBALDATA userInfoModel].Email.length > 0) {
        emailStr = [GLOBALDATA userInfoModel].Email;
    }
    else
    {
        emailStr = @"0";
    }
    
    
    NSArray *textArr = @[nameStr,telStr,emailStr];
    for (int i = 0; i< 3; i ++) {
        YYLabel *titlelabel= [YYLabel new];
        titlelabel.frame = CGRectMake(15, i * 50, 60, 50);
        titlelabel.text = titleArr[i];
        titlelabel.font = kFONT_TITLE(16);
        [midView addSubview:titlelabel];
        
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(titlelabel.right, i * 50, kWindowW - titlelabel.right - 15, 50)];
        if ([textArr[i] length] > 0 && [textArr[i] integerValue] != 0) {
            textField.text = textArr[i];
        }
        else
        {
            textField.placeholder = placeholderArr[i];
        }
        textField.textAlignment = NSTextAlignmentRight;
        textField.textColor = kRGBColor(150, 150, 150);
        textField.font = kFONT_TITLE(16);
        [midView addSubview:textField];
        [self.textFieldArr addObject:textField];
        
        CALayer *line1 = [CALayer layer];
        line1.backgroundColor = kRGBColor(150, 150, 150).CGColor;
        line1.frame = CGRectMake(15, (i+1) * 50 - 0.5, kWindowW - 30, 0.5);
        [midView.layer addSublayer:line1];
    }
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(15, midView.bottom + 80, kWindowW - 30, 40);
    [btn1 setTitle:@"保存" forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor redColor];
    btn1.titleLabel.font = kFONT_TITLE(16);
    [btn1 addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn1];
    
}

- (void)saveBtnClick:(UIButton *)btn {
    UITextField *nameField = self.textFieldArr[0]; // 姓名
    UITextField *phoneField = self.textFieldArr[1]; // 手机
    UITextField *emailField = self.textFieldArr[2]; // 邮箱
    
    if (![self validateBandCard:phoneField.text regularString:REG_PHONENUM_STR]) {
        [SVProgressHUD showErrorWithStatus:@"手机号格式不对！"];
        return;
    }
    
    if (![self validateBandCard:emailField.text regularString:REG_emailNUM_STR]) {
        [SVProgressHUD showErrorWithStatus:@"邮箱格式不对！"];
        return;
    }
    
    JUserInfoModel *userModel = [JUserInfoModel new];
    userModel.Id = [GLOBALDATA userInfoModel].Id;
    userModel.Username = nameField.text;
    userModel.Tel = phoneField.text;
    userModel.Email = emailField.text;
    
    [self updateUesrinfoRequest:userModel];
    
}

- (void)updateUesrinfoRequest:(JUserInfoModel *)userModel {
    NSString *url = @"Customers/UpdateCustomer";
    [JNetManager UpdateCustomerInfoRequedtUrl:url
                                  userInfoObj:userModel
                                      success:^(id response) {
                                          if ([response integerValue]== 1) {
                                              [[GLOBALDATA userInfoModel] setTel:userModel.Tel];
                                              [[GLOBALDATA userInfoModel] setUsername:userModel.Username];
                                              [[GLOBALDATA userInfoModel] setEmail:userModel.Email];
                                              
                                              [SVProgressHUD showSuccessWithStatus:@"更新成功"];
                                              
                                              [self.navigationController popViewControllerAnimated:YES];
                                          }
                                      } failure:^(NSString *err) {
                                          [SVProgressHUD showErrorWithStatus:err];
                                      }];
}

- (void)leftButtonClick:(UIButton *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSMutableArray *)textFieldArr
{
    if (!_textFieldArr) {
        _textFieldArr = [NSMutableArray array];
    }
    return _textFieldArr;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)validateBandCard:(NSString *)baneCard regularString:(NSString *)regularString
{
    BOOL flag;
    if (baneCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSPredicate *bandCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regularString];
    return [bandCardPredicate evaluateWithObject:baneCard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
